#!/bin/bash

#-------------------------------------------------------------------
# icecc 状态查看脚本
#
# 功能：
#   查看 icecc 调度器和编译节点的运行状态、连接信息、性能统计
#
# 使用方法：
#   ./icecc-status.sh [scheduler|node|all]
#
#   scheduler: 仅查看调度器状态
#   node:      仅查看编译节点状态
#   all:       查看所有状态（默认）
#
# 作者：NFServer Team
# 日期：2025-11-05
#-------------------------------------------------------------------

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
BOLD='\033[1m'
NC='\033[0m' # No Color

# 打印标题
print_title() {
    echo ""
    echo -e "${BOLD}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
    echo -e "${BOLD}$1${NC}"
    echo -e "${BOLD}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
}

log_info() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

log_warning() {
    echo -e "${YELLOW}[WARN]${NC} $1"
}

log_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# 查看调度器状态
show_scheduler_status() {
    print_title "📊 icecc 调度器状态"
    
    # 检查服务是否存在
    if ! systemctl list-unit-files | grep -q icecc-scheduler; then
        log_error "icecc-scheduler 服务未安装"
        echo ""
        log_info "安装方法："
        echo "  sudo ./ci/setup-icecc-scheduler.sh"
        return 1
    fi
    
    # 服务状态
    echo -e "${CYAN}▶ 服务状态：${NC}"
    if systemctl is-active --quiet icecc-scheduler; then
        echo -e "  ${GREEN}● 运行中${NC}"
        
        # 服务详情（兼容旧版 systemd）
        local pid=$(systemctl show -p MainPID icecc-scheduler 2>/dev/null | cut -d'=' -f2)
        local memory=$(ps -o rss= -p $pid 2>/dev/null | awk '{print $1/1024}')
        local uptime=$(systemctl show -p ActiveEnterTimestamp icecc-scheduler 2>/dev/null | cut -d'=' -f2)
        
        echo -e "  PID: ${pid}"
        echo -e "  内存: ${memory} MB"
        echo -e "  启动时间: ${uptime}"
    else
        echo -e "  ${RED}● 未运行${NC}"
        echo ""
        log_error "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
        log_error "icecc-scheduler 服务未运行"
        log_error "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
        echo ""
        
        # 查看失败原因
        echo "  ${CYAN}▶ 服务状态详情：${NC}"
        systemctl status icecc-scheduler | head -20 | sed 's/^/    /'
        echo ""
        
        # 查看错误日志
        echo "  ${CYAN}▶ 最近错误日志：${NC}"
        journalctl -u icecc-scheduler -n 10 --no-pager | sed 's/^/    /'
        echo ""
        
        log_error "快速修复："
        echo ""
        echo "  # 方法 1: 启动服务"
        echo "  sudo systemctl start icecc-scheduler"
        echo ""
        echo "  # 方法 2: 重新部署"
        echo "  git pull"
        echo "  sudo ./ci/setup-icecc-scheduler.sh"
        echo ""
        echo "  # 查看详细日志"
        echo "  sudo journalctl -u icecc-scheduler -n 50"
        echo ""
        log_error "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
        echo ""
        return 1
    fi
    
    echo ""
    
    # 配置信息
    echo -e "${CYAN}▶ 配置信息：${NC}"
    if [ -f /etc/sysconfig/icecc ]; then
        local scheduler_host=$(grep "^ICECC_SCHEDULER_HOST" /etc/sysconfig/icecc | cut -d'=' -f2)
        local netname=$(grep "^ICECC_NETNAME" /etc/sysconfig/icecc | cut -d'=' -f2)
        echo -e "  监听地址: ${scheduler_host}"
        echo -e "  网络名称: ${netname}"
        echo -e "  配置文件: /etc/sysconfig/icecc"
    fi
    
    echo ""
    
    # 端口监听
    echo -e "${CYAN}▶ 端口监听：${NC}"
    if netstat -tuln 2>/dev/null | grep -q ":8765"; then
        echo -e "  ${GREEN}✅ 调度器端口 8765 已监听${NC}"
        netstat -tuln | grep ":8765" | head -1
    elif ss -tuln 2>/dev/null | grep -q ":8765"; then
        echo -e "  ${GREEN}✅ 调度器端口 8765 已监听${NC}"
        ss -tuln | grep ":8765" | head -1
    else
        echo -e "  ${RED}❌ 端口 8765 未监听${NC}"
    fi
    
    echo ""
    
    # 已连接的编译节点
    echo -e "${CYAN}▶ 已连接的编译节点：${NC}"
    local connected_nodes=$(journalctl -u icecc-scheduler --since "1 hour ago" | grep -i "connected" | tail -10)
    if [ -n "$connected_nodes" ]; then
        echo "$connected_nodes" | while IFS= read -r line; do
            # 提取节点信息
            if echo "$line" | grep -q "Node.*connected"; then
                local node_info=$(echo "$line" | grep -oP 'Node \K[^ ]+' || echo "$line" | awk '{print $NF}')
                echo -e "  ${GREEN}✅${NC} $node_info"
            fi
        done
    else
        echo -e "  ${YELLOW}暂无节点连接${NC}"
        echo ""
        log_warning "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
        log_warning "调度器运行中，但没有编译节点连接"
        log_warning "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
        echo ""
        
        # 检查调度器实际进程
        echo "  ${CYAN}▶ 检查调度器进程：${NC}"
        local scheduler_proc=$(ps aux | grep icecc-scheduler | grep -v grep)
        if [ -n "$scheduler_proc" ]; then
            echo "    ${GREEN}✅ 调度器进程存在${NC}"
            echo "$scheduler_proc" | sed 's/^/      /'
        else
            echo "    ${RED}❌ 调度器进程不存在（服务可能未正常启动）${NC}"
        fi
        
        echo ""
        echo "  ${CYAN}▶ 可能的原因：${NC}"
        echo "    1. 编译节点未部署或未启动"
        echo "    2. 编译节点配置的调度器地址错误"
        echo "    3. 网络名称（ICECC_NETNAME）不一致"
        echo "    4. 防火墙阻挡了连接"
        echo "    5. 调度器虽然服务启动，但实际进程未运行"
        echo ""
        
        log_info "部署编译节点："
        echo ""
        echo "  # 在每台编译服务器上运行："
        echo "  sudo ./ci/setup-icecc-node.sh 172.18.63.156:8765"
        echo ""
        echo "  # 验证编译节点："
        echo "  ./ci/icecc-status.sh node"
        echo ""
        
        log_info "检查调度器实际运行状态："
        echo ""
        echo "  # 查看调度器进程："
        echo "  ps aux | grep icecc-scheduler"
        echo ""
        echo "  # 手动启动调度器测试："
        echo "  sudo /usr/sbin/icecc-scheduler -n nfshm-build -p 8765 -v"
        echo ""
        echo "  # 查看详细日志："
        echo "  sudo journalctl -u icecc-scheduler -n 100 --no-pager"
        echo ""
        log_warning "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
        echo ""
    fi
    
    echo ""
    
    # 检查调度器实际进程（重要！）
    echo -e "${CYAN}▶ 调度器进程检查：${NC}"
    local scheduler_proc=$(ps aux | grep -E 'icecc-scheduler|scheduler' | grep -v grep)
    if [ -n "$scheduler_proc" ]; then
        echo -e "  ${GREEN}✅ 调度器进程正在运行${NC}"
        echo "$scheduler_proc" | sed 's/^/    /'
    else
        echo -e "  ${RED}❌ 调度器进程不存在${NC}"
        echo ""
        log_error "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
        log_error "调度器服务虽然显示运行中，但实际进程不存在！"
        log_error "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
        echo ""
        log_error "这通常是配置问题或调度器启动失败"
        echo ""
        log_error "快速修复："
        echo ""
        echo "  # 1. 手动启动调度器测试："
        echo "  sudo /usr/sbin/icecc-scheduler -n nfshm-build -p 8765 -v"
        echo ""
        echo "  # 2. 查看启动错误："
        echo "  sudo journalctl -u icecc-scheduler -n 100 --no-pager | less"
        echo ""
        echo "  # 3. 检查配置文件："
        echo "  cat /etc/sysconfig/icecc"
        echo ""
        echo "  # 4. 重新部署："
        echo "  git pull"
        echo "  sudo ./ci/setup-icecc-scheduler.sh"
        echo ""
        log_error "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
        echo ""
    fi
    
    echo ""
    
    # 最近日志
    echo -e "${CYAN}▶ 最近 10 条日志：${NC}"
    journalctl -u icecc-scheduler -n 10 --no-pager | tail -10 | sed 's/^/  /'
}

# 查看编译节点状态
show_node_status() {
    print_title "🖥️  icecc 编译节点状态"
    
    # 检查服务是否存在
    if ! systemctl list-unit-files | grep -q iceccd; then
        log_error "iceccd 服务未安装"
        echo ""
        log_info "安装方法："
        echo "  sudo ./ci/setup-icecc-node.sh <调度器IP>:8765"
        return 1
    fi
    
    # 服务状态
    echo -e "${CYAN}▶ 服务状态：${NC}"
    if systemctl is-active --quiet iceccd; then
        echo -e "  ${GREEN}● 运行中${NC}"
        
        # 服务详情（兼容旧版 systemd）
        local pid=$(systemctl show -p MainPID iceccd 2>/dev/null | cut -d'=' -f2)
        local memory=$(ps -o rss= -p $pid 2>/dev/null | awk '{print $1/1024}')
        local uptime=$(systemctl show -p ActiveEnterTimestamp iceccd 2>/dev/null | cut -d'=' -f2)
        
        echo -e "  PID: ${pid}"
        echo -e "  内存: ${memory} MB"
        echo -e "  启动时间: ${uptime}"
    else
        echo -e "  ${RED}● 未运行${NC}"
        echo ""
        log_error "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
        log_error "iceccd 服务未运行"
        log_error "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
        echo ""
        
        # 查看失败原因
        echo "  ${CYAN}▶ 服务状态详情：${NC}"
        systemctl status iceccd | head -20 | sed 's/^/    /'
        echo ""
        
        # 查看错误日志
        echo "  ${CYAN}▶ 最近错误日志：${NC}"
        journalctl -u iceccd -n 10 --no-pager | sed 's/^/    /'
        echo ""
        
        log_error "快速修复："
        echo ""
        echo "  # 方法 1: 修复权限问题（最常见）⭐"
        echo "  git pull"
        echo "  sudo ./ci/fix-icecc-permissions.sh"
        echo ""
        echo "  # 方法 2: 重启服务"
        echo "  sudo systemctl restart iceccd"
        echo ""
        echo "  # 方法 3: 重新部署"
        echo "  git pull"
        echo "  sudo ./ci/setup-icecc-node.sh <调度器IP>:8765"
        echo ""
        echo "  # 查看详细日志"
        echo "  sudo journalctl -u iceccd -n 50"
        echo ""
        log_error "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
        echo ""
        return 1
    fi
    
    echo ""
    
    # 配置信息
    echo -e "${CYAN}▶ 配置信息：${NC}"
    if [ -f /etc/sysconfig/icecc ]; then
        local scheduler_host=$(grep "^ICECC_SCHEDULER_HOST" /etc/sysconfig/icecc | cut -d'=' -f2)
        local netname=$(grep "^ICECC_NETNAME" /etc/sysconfig/icecc | cut -d'=' -f2)
        local max_jobs=$(grep "^ICECC_MAX_JOBS" /etc/sysconfig/icecc | cut -d'=' -f2)
        echo -e "  调度器地址: ${scheduler_host}"
        echo -e "  网络名称: ${netname}"
        echo -e "  最大任务数: ${max_jobs}"
        echo -e "  配置文件: /etc/sysconfig/icecc"
    fi
    
    echo ""
    
    # 端口监听
    echo -e "${CYAN}▶ 端口监听：${NC}"
    if netstat -tuln 2>/dev/null | grep -q ":10245"; then
        echo -e "  ${GREEN}✅ 编译节点端口 10245 已监听${NC}"
        netstat -tuln | grep ":10245" | head -1
    elif ss -tuln 2>/dev/null | grep -q ":10245"; then
        echo -e "  ${GREEN}✅ 编译节点端口 10245 已监听${NC}"
        ss -tuln | grep ":10245" | head -1
    else
        echo -e "  ${RED}❌ 端口 10245 未监听${NC}"
    fi
    
    echo ""
    
    # 连接状态
    echo -e "${CYAN}▶ 调度器连接状态：${NC}"
    local connection_log=$(journalctl -u iceccd --since "1 hour ago" | grep -i "connect\|scheduler")
    if echo "$connection_log" | grep -q "Connected to scheduler"; then
        echo -e "  ${GREEN}✅ 已连接到调度器${NC}"
        echo "$connection_log" | grep "Connected to scheduler" | tail -1 | sed 's/^/  /'
    else
        echo -e "  ${RED}❌ 未连接到调度器${NC}"
        echo ""
        log_error "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
        log_error "问题诊断："
        echo ""
        
        # 读取配置
        local scheduler_host=""
        if [ -f /etc/sysconfig/icecc ]; then
            scheduler_host=$(grep "^ICECC_SCHEDULER_HOST" /etc/sysconfig/icecc | cut -d'=' -f2)
        fi
        
        if [ -n "$scheduler_host" ]; then
            local scheduler_ip=$(echo "$scheduler_host" | cut -d':' -f1)
            local scheduler_port=$(echo "$scheduler_host" | cut -d':' -f2)
            
            echo "  目标调度器: ${scheduler_host}"
            echo ""
            
            # 测试网络连接
            echo "  1️⃣ 测试网络连通性..."
            if ping -c 2 -W 2 "$scheduler_ip" &>/dev/null; then
                echo "    ${GREEN}✅ Ping 成功${NC}"
            else
                echo "    ${RED}❌ Ping 失败 - 网络不通${NC}"
            fi
            
            echo ""
            echo "  2️⃣ 测试端口连接..."
            if timeout 3 bash -c "cat < /dev/null > /dev/tcp/${scheduler_ip}/${scheduler_port}" 2>/dev/null; then
                echo "    ${GREEN}✅ 端口 ${scheduler_port} 可连接${NC}"
            else
                echo "    ${RED}❌ 端口 ${scheduler_port} 无法连接${NC}"
                echo "    可能原因："
                echo "      • 调度器服务未运行"
                echo "      • 防火墙/安全组未开放端口 ${scheduler_port}"
            fi
            
            echo ""
            log_error "快速修复："
            echo ""
            echo "  # 在调度器节点（${scheduler_ip}）执行："
            echo "  sudo systemctl status icecc-scheduler"
            echo "  sudo systemctl restart icecc-scheduler"
            echo ""
            echo "  # 或使用状态查看脚本："
            echo "  ssh root@${scheduler_ip} 'cd /path/to/nfshm-xframe && ./ci/icecc-status.sh scheduler'"
            echo ""
            echo "  # 检查端口是否开放："
            echo "  telnet ${scheduler_ip} ${scheduler_port}"
            echo ""
            echo "  # 运行完整诊断："
            echo "  ./ci/diagnose-icecc-connection.sh ${scheduler_host}"
            echo ""
        else
            echo "  未找到调度器配置"
            echo "  请检查 /etc/sysconfig/icecc 文件"
        fi
        
        log_error "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    fi
    
    echo ""
    
    # 当前编译任务
    echo -e "${CYAN}▶ 当前编译任务：${NC}"
    local active_tasks=$(ps aux | grep -E 'cc1|cc1plus|g\+\+.*icecc' | grep -v grep | wc -l)
    if [ "$active_tasks" -gt 0 ]; then
        echo -e "  ${GREEN}活动任务数: ${active_tasks}${NC}"
        ps aux | grep -E 'cc1|cc1plus|g\+\+.*icecc' | grep -v grep | head -5 | awk '{print "    "$2, $11}' || true
    else
        echo -e "  ${YELLOW}无活动任务${NC}"
    fi
    
    echo ""
    
    # CPU 和内存使用
    echo -e "${CYAN}▶ 系统资源：${NC}"
    local cpu_usage=$(top -bn1 | grep "Cpu(s)" | awk '{print $2}' | cut -d'%' -f1)
    local mem_total=$(free -h | grep Mem | awk '{print $2}')
    local mem_used=$(free -h | grep Mem | awk '{print $3}')
    local load_avg=$(uptime | awk -F'load average:' '{print $2}' | xargs)
    
    echo -e "  CPU 使用率: ${cpu_usage}%"
    echo -e "  内存使用: ${mem_used} / ${mem_total}"
    echo -e "  负载: ${load_avg}"
    
    echo ""
    
    # 最近日志
    echo -e "${CYAN}▶ 最近 5 条日志：${NC}"
    journalctl -u iceccd -n 5 --no-pager | tail -5 | sed 's/^/  /'
}

# 查看网络连接测试
test_connectivity() {
    print_title "🔗 网络连接测试"
    
    # 从配置文件读取调度器地址
    local scheduler_host=""
    if [ -f /etc/sysconfig/icecc ]; then
        scheduler_host=$(grep "^ICECC_SCHEDULER_HOST" /etc/sysconfig/icecc | cut -d'=' -f2)
    fi
    
    if [ -z "$scheduler_host" ]; then
        log_warning "未找到调度器配置"
        return 1
    fi
    
    local scheduler_ip=$(echo "$scheduler_host" | cut -d':' -f1)
    local scheduler_port=$(echo "$scheduler_host" | cut -d':' -f2)
    
    echo -e "${CYAN}▶ 测试调度器连接：${NC}"
    echo -e "  目标: ${scheduler_ip}:${scheduler_port}"
    
    # 测试连接
    if timeout 3 bash -c "cat < /dev/null > /dev/tcp/${scheduler_ip}/${scheduler_port}" 2>/dev/null; then
        echo -e "  ${GREEN}✅ 可以连接到调度器${NC}"
    else
        echo -e "  ${RED}❌ 无法连接到调度器${NC}"
        log_error "请检查："
        echo "    1. 调度器服务是否运行"
        echo "    2. 网络是否畅通"
        echo "    3. 防火墙/安全组是否开放端口 ${scheduler_port}"
    fi
    
    echo ""
    
    # Ping 测试
    echo -e "${CYAN}▶ Ping 测试：${NC}"
    if ping -c 3 -W 2 "$scheduler_ip" &>/dev/null; then
        local ping_result=$(ping -c 3 -W 2 "$scheduler_ip" | tail -1)
        echo -e "  ${GREEN}✅ 网络可达${NC}"
        echo -e "  $ping_result" | sed 's/^/  /'
    else
        echo -e "  ${RED}❌ 网络不可达${NC}"
    fi
}

# 显示统计信息
show_statistics() {
    print_title "📈 icecc 统计信息"
    
    echo -e "${CYAN}▶ 调度器统计（最近1小时）：${NC}"
    
    # 统计连接事件
    local connects=$(journalctl -u icecc-scheduler --since "1 hour ago" | grep -c "connected" || echo "0")
    local disconnects=$(journalctl -u icecc-scheduler --since "1 hour ago" | grep -c "disconnect" || echo "0")
    
    echo -e "  节点连接: ${connects} 次"
    echo -e "  节点断开: ${disconnects} 次"
    
    echo ""
    
    if systemctl is-active --quiet iceccd; then
        echo -e "${CYAN}▶ 编译节点统计（最近1小时）：${NC}"
        
        # 统计编译任务
        local compile_count=$(journalctl -u iceccd --since "1 hour ago" | grep -c "compil" || echo "0")
        
        echo -e "  编译相关日志: ${compile_count} 条"
    fi
    
    echo ""
    
    # 系统资源历史
    echo -e "${CYAN}▶ 系统负载历史：${NC}"
    if command -v sar &> /dev/null; then
        sar -u 1 3 | tail -4
    else
        uptime
    fi
}

# 实时监控
monitor_realtime() {
    print_title "📺 icecc 实时监控（按 Ctrl+C 退出）"
    
    local mode="$1"
    
    if [ "$mode" = "scheduler" ]; then
        log_info "实时监控调度器日志..."
        journalctl -u icecc-scheduler -f
    elif [ "$mode" = "node" ]; then
        log_info "实时监控编译节点日志..."
        journalctl -u iceccd -f
    else
        log_info "实时监控所有 icecc 日志..."
        journalctl -u icecc-scheduler -u iceccd -f
    fi
}

# 主函数
main() {
    local mode="${1:-all}"
    
    print_title "🎯 icecc 分布式编译状态查看工具"
    
    echo ""
    echo -e "当前时间: ${CYAN}$(date '+%Y-%m-%d %H:%M:%S')${NC}"
    echo -e "主机名: ${CYAN}$(hostname)${NC}"
    echo -e "IP 地址: ${CYAN}$(hostname -I | awk '{print $1}')${NC}"
    
    case "$mode" in
        scheduler)
            show_scheduler_status
            show_statistics
            echo ""
            read -p "是否实时监控调度器日志？(y/N): " monitor
            if [[ "$monitor" =~ ^[Yy]$ ]]; then
                monitor_realtime "scheduler"
            fi
            ;;
        node)
            show_node_status
            test_connectivity
            show_statistics
            echo ""
            read -p "是否实时监控编译节点日志？(y/N): " monitor
            if [[ "$monitor" =~ ^[Yy]$ ]]; then
                monitor_realtime "node"
            fi
            ;;
        all|*)
            # 检测当前是什么角色
            local has_scheduler=false
            local has_node=false
            
            if systemctl list-unit-files | grep -q icecc-scheduler; then
                has_scheduler=true
            fi
            
            if systemctl list-unit-files | grep -q iceccd; then
                has_node=true
            fi
            
            # 显示状态
            if [ "$has_scheduler" = true ]; then
                show_scheduler_status
            fi
            
            if [ "$has_node" = true ]; then
                show_node_status
                test_connectivity
            fi
            
            if [ "$has_scheduler" = false ] && [ "$has_node" = false ]; then
                log_error "未检测到 icecc 调度器或编译节点服务"
                echo ""
                log_info "安装方法："
                echo "  • 调度器: sudo ./ci/setup-icecc-scheduler.sh"
                echo "  • 编译节点: sudo ./ci/setup-icecc-node.sh <调度器IP>:8765"
                exit 1
            fi
            
            show_statistics
            
            echo ""
            read -p "是否实时监控日志？(y/N): " monitor
            if [[ "$monitor" =~ ^[Yy]$ ]]; then
                monitor_realtime "all"
            fi
            ;;
    esac
    
    echo ""
    print_title "✅ 状态查看完成"
    
    echo ""
    echo "常用命令："
    echo "  • 查看调度器状态: $0 scheduler"
    echo "  • 查看编译节点状态: $0 node"
    echo "  • 查看所有状态: $0 all"
    echo "  • 实时监控日志: journalctl -u icecc-scheduler -u iceccd -f"
    echo ""
}

# 执行主函数
main "$@"

